package xx.services.page.rest;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;

import xx.entity.page.PageItemView;
import xx.services.Passport;
import xx.services.page.inter.IViewInterface;
import xx.services.util.Constants;
import xx.util.string.Format;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

/**
 * page页面预览实体管理服务接口
 * 
 * @Path (Constants.RESTFUL_ROOT_PATH + "/page/view")
 * @author dengjinye,2010-12-22 $LastChangedDate$ $Author$
 */
@Path(Constants.RESTFUL_ROOT_PATH + "/page/view")
public class ViewRest extends Passport implements IViewInterface
{

    public ViewRest(@Context
    HttpServletRequest request)
    {
        super(request);
        // TODO Auto-generated constructor stub
    }

    /**
     * 获取一个View<br>
     * 
     * @GET
     * @Path ("/{view_id}")
     * @param viewId-@PathParam("view_id")
     *            ViewID
     * @return View实体
     * @author chenxiang
     */
    @GET
    @Path("/{view_id}")
    public String get(@PathParam("view_id")
    long viewId)
    {
        try
        {
            PageItemView view = new PageItemView(qr, owner);
            view.setViewId(viewId).load();
            return ok(view);
        }
        catch (Exception e)
        {
            return error(e);
        }
    }

    /**
     * 获取一个View html代码
     * 
     * @GET
     * @Path ("/{view_id}/html")
     * @param viewId-@PathParam("view_id")
     *            View ID
     * @return View Html代码，不是json数据
     * @author chenxiang
     */
    @GET
    @Path("/{view_id}/html")
    public String getHtml(@PathParam("view_id")
    long viewId)
    {
        try
        {
            PageItemView view = new PageItemView(qr, owner);
            String html = view.setViewId(viewId).toHtml();
            return ok(html, HTML);
        }
        catch (Exception e)
        {
            return error(e);
        }
    }

    /**
     * 修改一个View demo。<br>
     * 
     * @PUT
     * @Path ("/{view_id}")
     * @param viewId-@PathParam("view_id")
     *            View ID
     * @param view-@FormParam("view")
     *            View demo的Json字符串
     * @return 新增的View 实体
     * @author chenxiang
     */
    @PUT
    @Path("/{view_id}")
    public String update(@PathParam("view_id")
    long viewId, @FormParam("view")
    String view, @FormParam("title")
    String title, @FormParam("container")
    String container, @FormParam("templet")
    String templet, @FormParam("demo")
    String demo, @FormParam("shape")
    String shape, @FormParam("description")
    String description, @FormParam("sign")
    int sign, @FormParam("clazz")
    String clazz, @FormParam("clazz_id")
    long clazzId)
    {
        try
        {
            PageItemView piv = new PageItemView(qr, owner);
            piv.setViewId(viewId);
            if (Format.noEmpty(view))
            {
                JsonParser parser = new JsonParser();
                JsonObject jo = parser.parse(view).getAsJsonObject();
                piv.update(jo);
            }
            else
            {
                StringBuilder fields = new StringBuilder();
                if (Format.noEmpty(title))
                {
                    fields.append(PageItemView.TITLE).append(",");
                    piv.setTitle(title);
                }
                if (Format.noEmpty(container))
                {
                    fields.append(PageItemView.CONTAINER).append(",");
                    piv.setContainer(container);
                }
                if (Format.noEmpty(templet))
                {
                    fields.append(PageItemView.TEMPLET).append(",");
                    piv.setTemplet(templet);
                }
                if (Format.noEmpty(demo))
                {
                    fields.append(PageItemView.DEMO).append(",");
                    piv.setDemo(demo);
                }
                if (Format.noEmpty(shape))
                {
                    fields.append(PageItemView.SHAPE).append(",");
                    piv.setShape(shape);
                }
                if (Format.noEmpty(description))
                {
                    fields.append(PageItemView.DESCRIPTION).append(",");
                    piv.setDescription(description);
                }
                if (sign > 0)
                {
                    fields.append(PageItemView.SIGN).append(",");
                    piv.setSign(sign);
                }
                if (Format.noEmpty(clazz))
                {
                    fields.append(PageItemView.CLAZZ).append(",");
                    piv.setClazz(clazz);
                }
                if (clazzId > 0)
                {
                    fields.append(PageItemView.CLAZZ_ID).append(",");
                    piv.setClazzId(clazzId);
                }
                piv.update(fields.toString());
            }
            return ok(piv);
        }
        catch (Exception e)
        {
            return error(e);
        }
    }

}
